#include <iostream>
#include <ctime>
#include <memory>
#include <string>
#include <cstring>
#include <fstream>
#include "Format.hpp"
#include "Message.hpp"
#include "Logger.hpp"
#include "Sink.hpp"
#include "Level.hpp"
#include "Buffer.hpp"
#include "Logs.hpp"

// void testLogs()
// {
//     size_t count = 0;

//     while(count < 50000)
//     {
//         Logs::DEBUG("测试日志%d",count);
//         Logs::INFO("测试日志%d",count);
//         Logs::WARN("测试日志%d",count);
//         Logs::FATAL("测试日志%d",count);
//         ++count;
//     }
// }

void testLogger()
{
    // Logs::Logger::Ptr logger = Logs::LoggerManager::getInc().getLogger("async_logger");

    // logger->debug("%s","测试日志");
    // logger->warn("%s","测试日志");
    // logger->error("%s","测试日志");
    // logger->fatal("%s","测试日志");

    DEBUG("%s","测试日志。");

    size_t count = 0;
    while(++count < 500)
    {
        DEBUG("%s","测试日志。");
    }

    // const char* s = "Hello Log\n";
    // b.push(s,sizeof(s));

    // //logger->realLog(b);
    // logger->log(s,sizeof(s));

    // int count = 0;

    // while(count < 5000)
    // {
    //     logger->debug(__FILE__,__LINE__,"测试日志%d\n",count);
    //     logger->info(__FILE__,__LINE__,"测试日志%d\n",count);
    //     logger->warn(__FILE__,__LINE__,"测试日志%d\n",count);
    //     logger->fatal(__FILE__,__LINE__,"测试日志%d\n",count);
    //     ++count;
    // }
}

int main()
{
    //Logs::LoggerType::LOGGER_SYNC,Logs::Level::Value::DEBUG,"Sync1",Logs::AsyncType::ASYNC_SAFE)
    // std::unique_ptr<Logs::LoggerBuilder> build(std::make_unique<Logs::LocalLoggerBuilder>());
    // build->BuildLoggerName("ASync-1");
    // build->BuildLoggerType(Logs::LoggerType::LOGGER_ASYNC);
    // build->BuildAsyncUnsafe();
    // build->BuildLoggerLevel(Logs::Level::Value::DEBUG);
    // build->BuildFormatter("%s %d %m %n");

    // Logs::Message m(Logs::Until::Time::GetTime(),Logs::Level::Value::DEBUG,"main.cc",54,"格式化测试","[wangrui]");

    // build->BuildSink<Logs::StdoutSink>();
    // build->BuildSink<Logs::FileSink>("./acdc/wangrui-Filelog.log");
    // build->BuildSink<Logs::RollBySizeSink>("./acdc/wangrui-Rolllog",100);

    // Logs::Logger::Ptr logger = build->Build();

    // logger->debug(__FILE__,__LINE__,"测试日志",0);
    // logger->info(__FILE__,__LINE__,"测试日志",0);
    // logger->warn(__FILE__,__LINE__,"测试日志",0);
    // logger->fatal(__FILE__,__LINE__,"测试日志",0);

    // size_t count = 0;
    // while(count < 50000)
    // {
    //     logger->debug(__FILE__,__LINE__,"测试日志%d",count);
    //     logger->info(__FILE__,__LINE__,"测试日志%d",count);
    //     logger->warn(__FILE__,__LINE__,"测试日志%d",count);
    //     logger->fatal(__FILE__,__LINE__,"测试日志%d",count);
    //     ++count;
    // }

    // std::unique_ptr<Logs::LoggerBuilder> build(new Logs::GlobalLoggerBuilder());

    // build->BuildLoggerName("async_logger");
    // build->BuildLoggerLevel(Logs::Level::Value::WARN);
    // //build->BuildFormatter("[%c]%m%n");
    // build->BuildLoggerType(Logs::LoggerType::LOGGER_ASYNC);
    // build->BuildAsyncUnsafe();
    // build->BuildSink<Logs::FileSink>("./logfile/async.log");
    // build->BuildSink<Logs::StdoutSink>();
    // build->Build();

    testLogger();

    // std::string filename = "./logs/test.log";
    // Logs::Until::File::createdirectory(Logs::Until::File::path(filename));
    // // 读取文件数据，一点一点写入缓冲区，最终将缓冲区文件写入文件，判断生成的新文件与源文件是否一致。
    // std::ifstream ifs(filename,std::ios::binary | std::ios::app);
    // if(ifs.is_open() == false) { return -1; }
    // // ifs.seekg(0,std::ios::end);
    // // size_t fsize = ifs.tellg();
    // // ifs.seekg(0,std::ios::beg);
    // std::string body("LOGS-System");
    // // std::cout << fsize << std::endl;
    // // body.resize(fsize);
    // // ifs.read(&body[0],fsize);
    // if(ifs.good() == false) { std::cout << "read error\n"; return -1; }
    // ifs.close();

    // body = "LOGS-System";

    // Logs::Buffer buffer;
    // std::cout << body.size() << std::endl;
    // for(int i = 0;i < body.size();++i)
    // {
    //     buffer.push(&body[i],1);
    // }

    // std::cout << buffer.readAbleLen() << std::endl;
    // std::ofstream ofs(filename,std::ios::binary | std::ios::app);
    // size_t len = buffer.readAbleLen();
    // for(int i = 0;i < len;++i)
    // {
    //     if(ofs.good() == false) { std::cout << "Write Fail!" << std::endl ; return -1; }
    //     ofs.write(buffer.begin(),1);
    //     buffer.moveRead(1);
    // }

    // Logs::LoggerBuilder::Ptr build(std::make_shared<Logs::LocalLoggerBuilder>(Logs::LoggerType::LOGGER_SYNC,Logs::Level::Value::DEBUG,"Sync1",Logs::AsyncType::ASYNC_SAFE));
    // build->BuildLoggerType(Logs::LoggerType::LOGGER_SYNC);
    // build->BuildLoggerLevel(Logs::Level::Value::DEBUG);
    // build->BuildLoggerName("Sync-1");

    // Logs::Message m(Logs::Until::Time::GetTime(),Logs::Level::Value::DEBUG,"main.cc",54,"格式化测试","[wangrui]");
    // Logs::Formatter::Ptr formatter(new Logs::Formatter());
    // std::stringstream ss;
    // formatter->format(ss,m);

    // build->BuildFormatter(ss.str());

    // // Logs::Sink::Ptr stdout = Logs::SinkFactory::CreateSink<Logs::StdoutSink>();
    // // Logs::Sink::Ptr file = Logs::SinkFactory::CreateSink<Logs::FileSink>("./acdc/wangrui-Filelog");
    // // Logs::Sink::Ptr roll = Logs::SinkFactory::CreateSink<Logs::RollBySizeSink>("./acdc/wangrui-Rolllog",100);

    // build->BuildSink<Logs::StdoutSink>();
    // build->BuildSink<Logs::FileSink>("./acdc/wangrui-Filelog");
    // build->BuildSink<Logs::RollBySizeSink>("./acdc/wangrui-Rolllog",100);

    // Logs::Logger::Ptr logger = build->Build();

    // logger->debug(__FILE__,__LINE__,"测试日志");
    // logger->info(__FILE__,__LINE__,"测试日志");
    // logger->warn(__FILE__,__LINE__,"测试日志");
    // logger->fatal(__FILE__,__LINE__,"测试日志");

    // int count = 0;

    // while(count < 5000)
    // {
    //     logger->debug(__FILE__,__LINE__,"测试日志%d\n",count);
    //     logger->info(__FILE__,__LINE__,"测试日志%d\n",count);
    //     logger->warn(__FILE__,__LINE__,"测试日志%d\n",count);
    //     logger->fatal(__FILE__,__LINE__,"测试日志%d\n",count);
    //     ++count;
    // }

    // Level::Value& limit_level
    // ,Format::Ptr& formatter
    // ,const std::string& logger_name
    // ,std::vector<Sink:Ptr>& sinks

    // Logs::Level::Value level = Logs::Level::Value::DEBUG;
    // Logs::Formatter::Ptr formatter(new Logs::Formatter());
    // Logs::Sink::Ptr stdout = Logs::SinkFactory::CreateSink<Logs::StdoutSink>();
    // Logs::Sink::Ptr file = Logs::SinkFactory::CreateSink<Logs::FileSink>("./acdc/wangrui-Filelog");
    // Logs::Sink::Ptr roll = Logs::SinkFactory::CreateSink<Logs::RollBySizeSink>("./acdc/wangrui-Rolllog",100);


    // Logs::Formatter f;
    // Logs::Message m(time(nullptr),Logs::Level::Value::INFO,"main.cc",54,"格式化测试","[wangrui]");
    // std::string s = f.format(m);
    // std::shared_ptr<Logs::Sink> fs = Logs::SinkFactory::CreateSink<Logs::RollByTimeSink>("./acdc/wangrui/log",Logs::Time_Gap::GAP_SECOND);
    // char str[100];

    // time_t old = time(nullptr) + 5;
    // time_t cur = time(nullptr);
    
    // while(cur < old)
    // {
    //     strcpy(str,s.c_str());
    //     fs->log(str,sizeof(str));
    //     sleep(1);
    //     cur = time(nullptr);
    // }

    return 0;
}